import java.util.Arrays;

/**
* @description: 910. 最小差值 II
* @author hewei
* @date 2022/9/20 14:42
* @version 1.0
*/

public class SmallestRangeII {

    public int smallestRangeII(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int ans = nums[n - 1] - nums[0];
        for (int i = 0; i < n - 1; i++) {
            ans = Math.min(ans, Math.max(nums[i] + k, nums[n - 1] - k) - Math.min(nums[0] + k, nums[i + 1] - k));
        }
        return ans;
    }
}
